<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\PersonalAccountWithdraw;
use App\Models\User;
use App\Models\UserBankCard;

class PersonalAccountWithdrawRepository
{
    /**
     * 创建用户收益
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return PersonalAccountWithdraw::create($params);
    }

    /**
     * 查找一条提现记录
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return PersonalAccountWithdraw::find($id);
    }

    public function updateStatusByWithdrawNo($orderNo, $status)
    {
        return PersonalAccountWithdraw::where('withdraw_no', $orderNo)->update([
            'withdraw_status' => $status,
            'update_time' => time()
        ]);
    }

    public function byWithdrawNo($orderNo)
    {
        return PersonalAccountWithdraw::where('withdraw_no', $orderNo)->first();
    }

    public function getTradeRecordAll($userId, $page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        $offset = $page * $limit;
        return PersonalAccountWithdraw::where('user_id', $userId)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();
    }

    public function getTradeRecordToStatus($userId, $status, $page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        $offset = $page * $limit;
        return PersonalAccountWithdraw::where([
            'user_id' => $userId,
            'withdraw_status' => $status,
        ])
            ->offset($offset)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();
    }

    public function getList($request)
    {
        $page = $request->pageNumber - 1;
        $page = $page < 0 ? 0 : $page;
        $limit = $request->pageSize ? $request->pageSize : 50;
        $offset = $page * $limit;
        $query = PersonalAccountWithdraw::where(function ($query) use ($request) {
                if (!empty($request->withdraw_status)) {
                    $query->where('withdraw_status', $request->withdraw_status);
                }
                if (!empty($request->keyword)) {
                    switch ($request->field) {
                        case 'user_id':
                            $query->where('user_id', $request->keyword);
                            break;
                        case 'id':
                            $query->where('id', $request->keyword);
                            break;
                        case 'withdraw_no':
                            $query->where('withdraw_no', 'like', '%' . $request->keyword . '%');
                            break;
                        case 'idcard_number':
                            $userId = User::where('idcard_number', $request->keyword)->value('user_id');
                            $query->where('user_id', $userId);
                            break;
                        case 'bank_card_no':
                            $creditCardId = UserBankCard::where('bank_no', $request->keyword)->value('id');
                            $query->where('bank_card_id', $creditCardId);
                            break;
                        case 'bank_card_id':
                            $query->where('bank_card_id', $request->keyword);
                            break;
                    }
                }
                if (!empty($request->start_time) && !empty($request->end_time)) {
                    $query->whereBetween('create_time', [strtotime($request->start_time), strtotime($request->end_time)]);
                }
            });
        $total = $query->count();
        $result = $query->latest('id')
            ->offset($offset)
            ->limit($limit)
            ->get();
        return ['result' => $result, 'count' => $total];
    }

}